from django.db import models
from django.contrib.auth.models import User


class Notification(models.Model):
    """系统通知表"""
    NOTIFICATION_TYPE_CHOICES = [
        ('system', '系统通知'),
        ('emotion', '情绪提醒'),
        ('course', '课程通知'),
        ('community', '社区消息'),
        ('crisis', '危机预警'),
        ('activity', '活动提醒'),
    ]
    
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    notification_type = models.CharField(
        max_length=50, 
        choices=NOTIFICATION_TYPE_CHOICES, 
        verbose_name='通知类型',
        db_comment='通知消息类型'
    )
    title = models.CharField(
        max_length=200, 
        verbose_name='标题',
        db_comment='通知标题'
    )
    content = models.TextField(
        verbose_name='内容',
        db_comment='通知正文内容'
    )
    related_id = models.IntegerField(
        null=True, 
        blank=True, 
        verbose_name='关联ID',
        db_comment='关联的业务数据ID'
    )
    related_type = models.CharField(
        max_length=50, 
        null=True, 
        blank=True, 
        verbose_name='关联类型',
        db_comment='关联的业务数据类型'
    )
    is_read = models.BooleanField(
        default=False, 
        verbose_name='是否已读',
        db_comment='通知是否已读'
    )
    read_at = models.DateTimeField(
        null=True, 
        blank=True, 
        verbose_name='阅读时间',
        db_comment='通知阅读时间'
    )
    priority = models.IntegerField(
        default=1, 
        verbose_name='优先级',
        db_comment='通知优先级1-5(数字越大越重要)'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_notification'
        db_table_comment = '系统通知表'
        verbose_name = '系统通知'
        verbose_name_plural = '系统通知'
        ordering = ['-priority', '-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.title}'


class UserPreference(models.Model):
    """用户通知偏好设置表"""
    user = models.OneToOneField(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    enable_system = models.BooleanField(
        default=True, 
        verbose_name='系统通知',
        db_comment='是否接收系统通知'
    )
    enable_emotion = models.BooleanField(
        default=True, 
        verbose_name='情绪提醒',
        db_comment='是否接收情绪提醒'
    )
    enable_course = models.BooleanField(
        default=True, 
        verbose_name='课程通知',
        db_comment='是否接收课程通知'
    )
    enable_community = models.BooleanField(
        default=True, 
        verbose_name='社区消息',
        db_comment='是否接收社区消息'
    )
    enable_crisis = models.BooleanField(
        default=True, 
        verbose_name='危机预警',
        db_comment='是否接收危机预警(建议开启)'
    )
    enable_activity = models.BooleanField(
        default=True, 
        verbose_name='活动提醒',
        db_comment='是否接收活动提醒'
    )
    quiet_start_time = models.TimeField(
        null=True, 
        blank=True, 
        verbose_name='免打扰开始',
        db_comment='免打扰时段开始时间'
    )
    quiet_end_time = models.TimeField(
        null=True, 
        blank=True, 
        verbose_name='免打扰结束',
        db_comment='免打扰时段结束时间'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_user_preference'
        db_table_comment = '用户通知偏好设置表'
        verbose_name = '通知偏好设置'
        verbose_name_plural = '通知偏好设置'

    def __str__(self):
        return f'{self.user.username}的通知设置'
